<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/21 0021
 * Time: 下午 2:59
 * Author: NanTao <260053530@qq.com>
 */

namespace app\bjwlmanager\controller;

use think\Db;
class Goods extends ManagerBasic
{
    protected $channel_id = 2;
    /**
     * 加载产品信息列表
     */
    public function index()
    {
        $categoryId = input('param.category_id');
        $keywords = input('param.keywords');
        $where=[];
        $categoryId ? $where['category_id']=$categoryId :'';
        $keywords ? $where['title'] = ['like',"%{$keywords}%"]:'';
        $list = Db::name('goods')->where($where)->order('id desc')->paginate(15,false,['query'=>request()->param()])->each(function($item, $key){
            $categoryModel = get_category_model($item['category_id']);
            $item['categoryshow'] = $categoryModel['title'];
            return $item;
        });
        $this->assign('list',$list);
        $category = Db::name('sys_category')->where('channel_id',$this->channel_id)->select();
        $this->assign('category',$category);
        return view();
    }
    /*
     * 添加产品信息
     * */
    public function add()
    {
        if (request()->isPost())
        {
            $data = input('param.');
            $imgArray = $data['images'];
            unset($data['images']);
            $data['channel_id'] = $this->channel_id;
            $result = Db::name('goods')->insert($data);
            if ($result)
            {
                $id = Db::name('goods')->getLastInsID();
                if ($imgArray)
                {
                    foreach ($imgArray as $k=>$v) {
                        $ins[] = [
                            'main_id' => $id,
                            'img_url' => $v,
                            'thumb_picture' => $v,
                            'title' => $data['title'],
                            'type' => 1
                        ];
                    }
                    Db::name('img')->insertAll($ins);
                    json_msg('0','添加成功');
                }
            }
            else {
                json_msg(1,'添加失败');
            }
        }
        else {
            $category = Db::name('sys_category')->where('channel_id',2)->select();
            $this->assign('categoryList',$category);
            return $this->fetch();
        }
    }
    /*
     * 编辑产品信息
     * */
    public function edit()
    {
        $id = input('param.id');
        if (request()->isPost())
        {
            $data = input('param.');
            $imgArray = $data['images'];
            $data['is_rec'] = $data['is_rec']?$data['is_rec']:0;
            $data['is_top'] = $data['is_top']?$data['is_top']:0;
            $data['is_special'] = $data['is_special']?$data['is_special']:0;
            unset($data['images']);
            unset($data['id']);
            $data['channel_id'] = $this->channel_id;
            $result = Db::name('goods')->where('id',$id)->update($data);
            if ($result || $result == 0)
            {
                if ($imgArray)
                {
                    Db::name('img')->where(['main_id'=>$id,'type'=>1])->delete();
                    foreach ($imgArray as $k=>$v) {
                        $ins[] = [
                            'main_id' => $id,
                            'img_url' => $v,
                            'thumb_picture' => $v,
                            'title' => $data['title'],
                            'type' => 1
                        ];
                    }
                    Db::name('img')->insertAll($ins);
                }
                json_msg('0','编辑成功');
            }
            else {
                json_msg(1,'编辑失败');
            }
        }
        else {

            $category = Db::name('sys_category')->where('channel_id',2)->select();
            $this->assign('categoryList',$category);
            $info = Db::name('goods')->find($id);
            $this->assign('info',$info);
            $imgList = Db::name('img')->where(['main_id'=>$id,'type'=>1])->select();
            $this->assign('imgList',$imgList);
            return $this->fetch();
        }
    }
    public function show()
    {
        $id=input('get.id');
        $info=Db::name('service')->where('id',$id)->find();
        $this->assign('info',$info);
        return view();
    }


    //删除
    public function del()
    {
        $goodsId = input('post.goodsId');
        if(!isset($goodsId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('goods')->where('id',$goodsId)->delete();
        if($result==0)
        {
            json_msg(1,'删除失败');
        }
        else
        {
            json_msg(0,'删除成功');
        }
    }

    //停用
    public function stop()
    {
        $goodsId=input('post.goodsId');
        if(!isset($goodsId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('goods')->where('id',$goodsId)->setField('status',1);
        if($result==0)
        {
            json_msg(0,'状态未更改');
        }
        else
        {
            json_msg(0,'下架成功');
        }
    }

    //启用
    public function start()
    {
        $goodsId=input('post.goodsId');
        if(!isset($goodsId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('goods')->where('id',$goodsId)->setField('status',0);
        if($result==0)
        {
            json_msg(0,'状态未更改');
        }
        else
        {
            json_msg(0,'上架成功');
        }
    }
    public function comment()
    {
        $where = '';
        $list = Db::name('goods_comment')->where($where)->order('add_time desc')->paginate(15,false,['query'=>request()->param()])->each(function($item, $key){
            $user = get_user_model($item['user_id']);
            $item['user_nickname'] = $user['user_nickname'];
            $item['true_name'] = $user['true_name'];
            $item['images'] = array_filter(explode('|',$item['imglist']));
            return $item;
        });
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function commentdel()
    {
        $id = input('post.id');
        $result=Db::name('goods_comment')->where('id',$id)->delete();
        if($result==0) {
            json_msg(1,'删除失败');
        }
        else {
            json_msg(0,'删除成功');
        }
    }
}